﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

//importamos
using EventSys.BussinesEntities;
using EventSys.BussinesLogic;
using Microsoft.Practices.Unity;
using System.IO; // para usar el "Path"

namespace EventSys.MVC4Web.Controllers
{
    public class InversionController : Controller
    {
        [Dependency]
        public IInversionService inversionService { get; set; }

        [Dependency]
        public IEventoService eventoService { get; set; }

        //
        // GET: /Inversion/

        public ActionResult Index()
        {

            var lista = inversionService.GetFromAllInversion();

            return View();
        }

        public class listaInversion
        {
            public Int32 idInversion { get; set; }
            public Int32 IdEvents { get; set; }
            public Int32 IdActividad { get; set; }
            public string TipoAsistente { get; set; }
            public decimal Monto { get; set; }
        }

        public ActionResult viewDetInv(string asistente, decimal monto)
        {
            List<listaInversion> listaInv = new List<listaInversion>();

            //usuarios = usuarios.OrderByDescending(usuario => usuario.Puntos());

            //List<string> listaAsist = new List<string>();
            //List<decimal> listaMonto = new List<decimal>();

            string[][] lista = {};
                        
            //foreach (var li in listaInv)
            //{
            //    listaAsist.Add(li.TipoAsistente);
            //    listaMonto.Add(li.Monto);
            //}

            //for (var i = 0; i < 6; i++ )
            //{
                lista[0][0] = asistente;
                lista[0][1] = asistente;
            //}

            //ViewBag.Nombres = nombres;
            //ViewBag.Puntos = puntos;

            //return View(usuarios);

            //return View(lista);


            return PartialView("_detInversion", lista);

        }


        //********************  CREAR  *********************
        public ActionResult Create() {

            return View();
        }

        [HttpPost]
        public ActionResult Create(Inversion inversion)//, Evento evento)
        {
            //var idEvento = (from e in eventoService.GetAllFromEventos()
            //               select e.idEvents).Last();

            List<listaInversion> lista = new List<listaInversion>();

            Inversion listaInv = new Inversion();

            foreach (var li in lista)
            {
                //li.IdEvents = idEvento;
                li.TipoAsistente = inversion.TipoAsistente;
                li.Monto = inversion.Monto.Value;
                //listaInv;
            }

            inversionService.AddInversion(listaInv);

            return View();
        }


        // *****************   PROBANDO CON IMGEN    ******************
        
        [HttpPost]
        public ActionResult SendImage(HttpPostedFileBase img)
        {
            var data = new byte[img.ContentLength];
            img.InputStream.Read(data, 0, img.ContentLength);
            var path = ControllerContext.HttpContext.Server.MapPath("/");
            var filename = Path.Combine(path, Path.GetFileName(img.FileName));
            System.IO.File.WriteAllBytes(Path.Combine(path, filename), data);
            ViewBag.ImageUploaded = filename;

            return View("Index");
        }

        //para previsualizar la imagen
        public ActionResult Preview(string file)
        {
            var path = ControllerContext.HttpContext.Server.MapPath("/");
            if (System.IO.File.Exists(Path.Combine(path, file)))
            {
                return File(Path.Combine(path, file), "image/jpeg");
            }
            return new HttpNotFoundResult();
        }




        /////   otro metodo para la image... se realizara en el httppost del Index
        [HttpPost]
        public ActionResult Index(HttpPostedFileBase uploadfile1)
        {
            if (uploadfile1 != null)
            {
                if (uploadfile1.ContentLength > 0)
                {
                    var path = Path.Combine(Server.MapPath("~/Content"), uploadfile1.FileName);

                    ViewBag.myimage = uploadfile1.FileName;
                    uploadfile1.SaveAs(path);
                }
            }
            return View();
        }


    }
}
